//
//  SunSecondVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/12.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunSecondVC.h"
#import "SunSecondCell.h"
#import "MJRefresh.h"
#import "SunProductDetailVC.h"
#import "SunCycleProgress.h"
#import "SunVirVC.h"
#import "ColorButton.h"
#import "SelledViewController.h"
#undef Y
#define Y(y) AY(y)
@interface SunSecondVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tb;
    NSMutableArray *_dataArr;
    UIView *_test;
    int _page;
    Sun *_sun;
    //new
    UIView *_head;
    UILabel *_title;
    UIImageView *_stateSignal;
    UILabel *_basePercent;
    UILabel *_days;
    SunCycleProgress *_progress;
    UILabel *_startMoney;
    UILabel *_totalMoney;
}
@property (nonatomic, copy) NSString *over;
@property (nonatomic, copy) NSString *repay;

@end

@implementation SunSecondVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACKGROUND;
    self.title = @"项目";
    [self initData];
    [self creatUI];
    [_sun showActivity:self];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tb.mj_header beginRefreshing];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)initData{
    _dataArr = [NSMutableArray array];
    _sun = [[Sun alloc]init];
}

- (void)creatUI{
    _tb = [[UITableView alloc]initWithFrame:CGRectNull style:UITableViewStylePlain];
    _tb.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tb];
    [_tb lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.showsVerticalScrollIndicator = NO;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestToRefresh)];
    [self requestOverDate];
    
}
- (void)requestOverDate {
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWCCOUNT];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [_sun hideActivity];
        self.over = [requestDic objectForKey:@"over"];
        self.repay = [requestDic objectForKey:@"repay"];

        [_tb reloadData];

    } failure:^(NSString *errorInfo, int code) {
        [_sun hideActivity];

    }];

}
#pragma mark request
- (void)requestToRefresh{
    _page = 1;
    [self requestBorrowList];
}

- (void)requestBorrowList{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BORROWLIST];
    NSDictionary *param = @{@"page":@"1",@"per":@"100",@"status":@"1"};
    [HttpRequest postURL:url withParam:param success:^(id requestDic, NSString *msg) {
        [_sun hideActivity];
        [_tb.mj_header endRefreshing];

        [_dataArr removeAllObjects];
        NSArray *arr = [requestDic objectForKey:@"list"];
        [_dataArr addObjectsFromArray:arr];
        [_tb reloadData];
    } failure:^(NSString *errorInfo, int code) {
        [_sun hideActivity];
        [_tb.mj_header endRefreshing];

    }];
    
}

#pragma mark tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == _dataArr.count) {
        return 45;
    }
    return 115;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == _dataArr.count) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        [cell setBackgroundColor:[UIColor whiteColor]];
        
        UIButton *sellBtn = [[UIButton alloc]init];
        [sellBtn addTarget:self action:@selector(sellAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:sellBtn];
        [sellBtn setFrame:CGRectMake(0, 0, SCREENWIDTH/2, 45)];
        
        UIButton *overBtn = [[UIButton alloc]init];
        [overBtn addTarget:self action:@selector(overAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:overBtn];
        [overBtn setFrame:CGRectMake(SCREENWIDTH/2, 0, SCREENWIDTH/2, 45)];
        
        UIImageView *sellImage = [[UIImageView alloc]init];
        [sellBtn addSubview:sellImage];
        [sellImage setImage:[UIImage imageNamed:@"second_sell"]];
        [sellImage setFrame:CGRectMake(X(15), 14, 17, 17)];
        
        UIImageView *overImage = [[UIImageView alloc]init];
        [overBtn addSubview:overImage];
        [overImage setImage:[UIImage imageNamed:@"second_over"]];
        [overImage setFrame:CGRectMake(X(15), 14, 17, 17)];
        
        UILabel *overLabel = [[UILabel alloc]init];
        overLabel.font = [UIFont fontWithName:@"DINAlternate-Bold" size:15];
        [overLabel setFrame:CGRectMake(X(37), 13, SCREENWIDTH/2 - X(37), 18)];
        overLabel.textColor = COLOR_TITLE_MAIN;
        [sellBtn addSubview:overLabel];
        
        UILabel *repayLabel = [[UILabel alloc]initWithFrame:CGRectMake(X(37), 13, SCREENWIDTH/2 - X(37), 18)];
        repayLabel.font = [UIFont fontWithName:@"DINAlternate-Bold" size:15];
        repayLabel.textColor = COLOR_TITLE_MAIN;
        [overBtn addSubview:repayLabel];
        
        UIImageView *sellNext = [[UIImageView alloc]init];
        [sellBtn addSubview:sellNext];
        [sellNext setImage:[UIImage imageNamed:@"first_next"]];
        [sellNext setFrame:CGRectMake(X(167), 18, 5, 9)];
        
        UIImageView *overNext = [[UIImageView alloc]init];
        [overBtn addSubview:overNext];
        [overNext setImage:[UIImage imageNamed:@"first_next"]];
        [overNext setFrame:CGRectMake(X(167), 18, 5, 9)];
        
        UIImageView *line = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"second_line"]];
        [line setFrame:CGRectMake(SCREENWIDTH/2, 0, 3, 39)];
        [cell.contentView addSubview:line];
        if (self.over != NULL) {
            NSString *overString = [NSString stringWithFormat:@"已售罄产品  %@",self.over];
            NSMutableAttributedString *overAtt = [[NSMutableAttributedString alloc]initWithString:overString];
            NSRange overRange = [overString rangeOfString:@"已售罄产品"];
            [overAtt addAttribute:NSForegroundColorAttributeName value:COLOR_ALERT_DARK range:overRange];
            [overAtt addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:12] range:overRange];
            [overLabel setAttributedText:overAtt];
        }
        if (self.repay != NULL) {
            NSString *repayString = [NSString stringWithFormat:@"已还款产品  %@",self.repay];
            NSMutableAttributedString *repayAtt = [[NSMutableAttributedString alloc]initWithString:repayString];
            NSRange repayRange = [repayString rangeOfString:@"已还款产品"];
            [repayAtt addAttribute:NSForegroundColorAttributeName value:COLOR_ALERT_DARK range:repayRange];
            [repayAtt addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:12] range:repayRange];
            [repayLabel setAttributedText:repayAtt];
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        SunSecondCell *cell = [SunSecondCell cellWithTableView:tableView];
        NSDictionary *dic = _dataArr[indexPath.section];
        [cell setCellData:dic];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}
- (void)sellAction {
    SelledViewController *selledVC = [[SelledViewController alloc]init];
    selledVC.over = self.over;
    selledVC.repay = self.repay;
    selledVC.type = @"2";
    selledVC.title = @"已售罄";
    [self.navigationController pushViewController:selledVC animated:NO];

}
- (void)overAction {
    SelledViewController *selledVC = [[SelledViewController alloc]init];
    selledVC.over = self.over;
    selledVC.repay = self.repay;
    selledVC.type = @"3";
    selledVC.title = @"已还款";
    [self.navigationController pushViewController:selledVC animated:NO];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section != _dataArr.count) {
        SunProductDetailVC *vc = [[SunProductDetailVC alloc]init];
        vc.borrowId = [_dataArr[indexPath.section] objectForKey:@"borrowId"];
        if ([[_dataArr[indexPath.section] objectForKey:@"desc"] length] > 0) {
            vc.title = [NSString stringWithFormat:@"%@ %@(%@)",[_dataArr[indexPath.section] objectForKey:@"name"],[_dataArr[indexPath.section] objectForKey:@"number"],[_dataArr[indexPath.section] objectForKey:@"desc"]];
        }else {
            vc.title = [NSString stringWithFormat:@"%@ %@",[_dataArr[indexPath.section] objectForKey:@"name"],[_dataArr[indexPath.section] objectForKey:@"number"]];
        }
        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BACKGROUND;
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
